package com.phone580.app.process;

import java.util.List;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.phone580.app.R;
import com.phone580.app.entity.RunningAppInfoEntity;
import com.phone580.app.untilty.CommonUtility;

public class ProcessListAdapter extends BaseAdapter {
	private List<RunningAppInfoEntity> data;
	private LayoutInflater inflater;
	private ActivityManager am;
	private Context context;
	
	public ProcessListAdapter(Context context, List<RunningAppInfoEntity> data) {
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.data = data;
		this.context = context;
	}
	
	public void setActivityManager(ActivityManager am) {
		this.am = am;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public RunningAppInfoEntity getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder holder = null;
		if (v == null) {
			v = inflater.inflate(R.layout.user_process_item, null);
			holder = new ViewHolder();
			holder.ivIcon = (ImageView)v.findViewById(R.id.process_icon_iv);
			holder.tvName = (TextView)v.findViewById(R.id.process_name_tv);
			holder.tvSize = (TextView)v.findViewById(R.id.process_size_tv);
			holder.btnKill = (Button) v.findViewById(R.id.process_kill_btn);
//			holder.btnUninstall = (Button) v.findViewById(R.id.process_uninstall_btn);
			v.setTag(holder);
		}
		holder = (ViewHolder) v.getTag();
		final RunningAppInfoEntity entity = getItem(position);
		holder.ivIcon.setImageDrawable(entity.getAppIcon());
		holder.tvName.setText(entity.getAppLabel());
		holder.tvSize.setText("大小：" + entity.getMemSize() + "kb");
		holder.btnKill.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				am.killBackgroundProcesses(entity.getPkgName());
				((ProcessManageActivity)context).refresh();
//				 android.os.Process.killProcess(entity.getPid());
//				 ((ProcessManageActivity)context).refresh();
			}
		});
//		holder.btnUninstall.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				Uri packageURI = Uri.parse("package:" + entity.getPkgName());   
//				Intent uninstallIntent = new Intent
//				(Intent.ACTION_DELETE, packageURI);   
//				context.startActivity(uninstallIntent); 
//				CommonUtility.showInstalledAppDetails(context, entity.getPkgName());
//			}
//		});
//		holder.btnUninstall.setVisibility(View.GONE);
		return v;
	}
	
	class ViewHolder {
		public ImageView ivIcon;
		public TextView tvName;
		public TextView tvSize;
		public Button btnKill;
		public Button btnUninstall;
	}
}
